home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / slang_c.c < prev    next >
C/C++ Source or Header  |  1997-05-25  |  14KB  |  567 lines

  1. /* 
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.      Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     slang_c.c, Riccardo Facchetti (riccardo@cdc8g5.cdc.polimi.it)
  21.  
  22.       based on ncurses_ctl.c
  23.       slang library is more efficient than ncurses one.
  24.  
  25.     04/04/1995
  26.       - Initial, working version.
  27.  
  28.     15/04/1995
  29.       - Works with no-trace playing too; not the best way, but
  30.         it is the only way: slang 0.99.1 don't have a window management interface
  31.         and I don't want write one for it! :)
  32.         The problem is that I have set the no-scroll slang option so
  33.         when there are too much messages, the last ones are not displayed at
  34.         all. Tipically the last messages are warning for instruments not found
  35.         so this is no real problem (I hope :)
  36.       - Get the real size of screen we are running on
  37.  
  38.     */
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <unistd.h>
  43. #include <stdarg.h>
  44. #include <termios.h>
  45. #include <sys/ioctl.h>
  46.  
  47. #include <slang.h>
  48.  
  49. #include "config.h"
  50. #include "common.h"
  51. #include "instrum.h"
  52. #include "playmidi.h"
  53. #include "output.h"
  54. #include "controls.h"
  55.  
  56. /*
  57.  * For the set_color pairs (so called 'objects')
  58.  * see the ctl_open()
  59.  */
  60. #define SLsmg_normal()                        SLsmg_set_color(20)
  61. #define SLsmg_bold()                  SLsmg_set_color(21)
  62. #define SLsmg_reverse()                       SLsmg_set_color(22)
  63.  
  64. static void ctl_refresh(void);
  65. static void ctl_help_mode(void);
  66. static void ctl_total_time(int tt);
  67. static void ctl_master_volume(int mv);
  68. static void ctl_file_name(char *name);
  69. static void ctl_current_time(int ct);
  70. static void ctl_note(int v);
  71. static void ctl_program(int ch, int val);
  72. static void ctl_volume(int channel, int val);
  73. static void ctl_expression(int channel, int val);
  74. static void ctl_panning(int channel, int val);
  75. static void ctl_sustain(int channel, int val);
  76. static void ctl_pitch_bend(int channel, int val);
  77. static void ctl_reset(void);
  78. static int ctl_open(int using_stdin, int using_stdout);
  79. static void ctl_close(void);
  80. static int ctl_read(int32 *valp);
  81. static int cmsg(int type, int verbosity_level, char *fmt, ...);
  82.  
  83. /**********************************************/
  84. /* export the interface functions */
  85.  
  86. #define ctl slang_control_mode
  87.  
  88. ControlMode ctl= 
  89. {
  90.   "slang interface", 's',
  91.   1,0,0,
  92.   ctl_open, dumb_pass_playing_list, ctl_close, ctl_read, cmsg,
  93.   ctl_refresh, ctl_reset, ctl_file_name, ctl_total_time, ctl_current_time, 
  94.   ctl_note, 
  95.   ctl_master_volume, ctl_program, ctl_volume, 
  96.   ctl_expression, ctl_panning, ctl_sustain, ctl_pitch_bend
  97. };
  98.  
  99.  
  100. /***********************************************************************/
  101. /* foreground/background checks disabled since switching to curses */
  102. /* static int in_foreground=1; */
  103. static int ctl_helpmode=0;
  104. static int msg_row = 0;
  105.  
  106. static void _ctl_refresh(void)
  107. {
  108.   SLsmg_gotorc(0,0);
  109.   SLsmg_refresh();
  110. }
  111.  
  112. static void SLsmg_printfrc( int r, int c, char *fmt, ...) {
  113.   char p[1000];
  114.   va_list ap;
  115.  
  116.   SLsmg_gotorc( r, c);
  117.   va_start(ap, fmt);
  118.   vsprintf(p, fmt, ap);
  119.   va_end(ap);
  120.  
  121.   SLsmg_write_string (p);
  122. }
  123.  
  124. static void ctl_head(void)
  125. {
  126.   SLsmg_printfrc(0, 0, "TiMidity v" TIMID_VERSION);
  127.   SLsmg_printfrc(0,SLtt_Screen_Cols-52, "(C) 1995 Tuukka Toivonen <toivonen@clinet.fi>");
  128.   SLsmg_printfrc(1,0, "Press 'h' for help with keys, or 'q' to quit.");
  129. }
  130. static void ctl_refresh(void)
  131. {
  132.   if (ctl.trace_playing)
  133.     _ctl_refresh();
  134. }
  135.  
  136. static void ctl_help_mode(void)
  137. {
  138.   if (ctl_helpmode)
  139.     {
  140.       ctl_helpmode=0;
  141.  
  142. /*
  143.  * Clear the head zone and reprint head message.
  144.  */
  145.       SLsmg_gotorc(0,0);
  146.       SLsmg_erase_eol();
  147.       SLsmg_gotorc(1,0);
  148.       SLsmg_erase_eol();
  149.         ctl_head();
  150.       _ctl_refresh();
  151.     }
  152.   else
  153.     {
  154.       ctl_helpmode=1;
  155.         SLsmg_reverse();
  156.       SLsmg_gotorc(0,0);
  157.       SLsmg_erase_eol();
  158.       SLsmg_write_string(
  159.             "V=Louder    b=Skip back      "
  160.             "n=Next file      r=Restart file");
  161.  
  162.       SLsmg_gotorc(1,0);
  163.       SLsmg_erase_eol();
  164.       SLsmg_write_string(
  165.             "v=Softer    f=Skip forward   "
  166.             "p=Previous file  q=Quit program");
  167.         SLsmg_normal();
  168.       _ctl_refresh();
  169.     }
  170. }
  171.  
  172. static void ctl_total_time(int tt)
  173. {
  174.   int mins, secs=tt/play_mode->rate;
  175.   mins=secs/60;
  176.   secs-=mins*60;
  177.  
  178.   SLsmg_gotorc(4,6+6+3);
  179.   SLsmg_bold();
  180.   SLsmg_printf("%3d:%02d", mins, secs);
  181.   SLsmg_normal();
  182.   _ctl_refresh();
  183. }
  184.  
  185. static void ctl_master_volume(int mv)
  186. {
  187.   SLsmg_gotorc(4,SLtt_Screen_Cols-5);
  188.   SLsmg_bold();
  189.   SLsmg_printf("%03d %%", mv);
  190.   SLsmg_normal();
  191.   _ctl_refresh();
  192. }
  193.  
  194. static void ctl_file_name(char *name)
  195. {
  196.   SLsmg_gotorc(3,6);
  197.   SLsmg_erase_eol();
  198.   SLsmg_bold();
  199.   SLsmg_write_string(name);
  200.   SLsmg_normal();
  201.   _ctl_refresh();
  202. }
  203.  
  204. static void ctl_current_time(int ct)
  205. {
  206.   int i,v;
  207.   int mins, secs=ct/play_mode->rate;
  208.   if (!ctl.trace_playing) 
  209.     return;
  210.  
  211.   mins=secs/60;
  212.   secs-=mins*60;
  213.   SLsmg_gotorc(4,6);
  214.   SLsmg_bold();
  215.   SLsmg_printf("%3d:%02d", mins, secs);
  216.   v=0;
  217.   i=voices;
  218.   while (i--)
  219.     if (voice[i].status!=VOICE_FREE) v++;
  220.   SLsmg_gotorc(4,48);
  221.   SLsmg_printf("%2d", v);
  222.   SLsmg_normal();
  223.   _ctl_refresh();
  224. }
  225.  
  226. static void ctl_note(int v)
  227. {
  228.   int xl;
  229.   if (!ctl.trace_playing) 
  230.     return;
  231.   xl=voice[v].note%(SLtt_Screen_Cols-24);
  232.   SLsmg_gotorc(8+voice[v].channel,xl+3);
  233.   switch(voice[v].status)
  234.     {
  235.     case VOICE_DIE:
  236.       SLsmg_write_char(',');
  237.       break;
  238.     case VOICE_FREE: 
  239.       SLsmg_write_char('.');
  240.       break;
  241.     case VOICE_ON:
  242.         SLsmg_bold();
  243.       SLsmg_write_char('0'+(10*voice[v].velocity)/128); 
  244.       SLsmg_normal();
  245.       break;
  246.     case VOICE_OFF:
  247.     case VOICE_SUSTAINED:
  248.       SLsmg_write_char('0'+(10*voice[v].velocity)/128);
  249.       break;
  250.     }
  251. }
  252.  
  253. static void ctl_program(int ch, int val)
  254. {
  255.   if (!ctl.trace_playing) 
  256.     return;
  257.   SLsmg_gotorc(8+ch, SLtt_Screen_Cols-20);
  258.   if (ISDRUMCHANNEL(ch))
  259.     {
  260.         SLsmg_bold();
  261.       SLsmg_printf("%03d", val);
  262.         SLsmg_normal();
  263.     }
  264.   else
  265.     SLsmg_printf("%03d", val);
  266. }
  267.  
  268. static void ctl_volume(int channel, int val)
  269. {
  270.   if (!ctl.trace_playing) 
  271.     return;
  272.   SLsmg_gotorc(8+channel, SLtt_Screen_Cols-16);
  273.   SLsmg_printf("%3d", (val*100)/127);
  274. }
  275.  
  276. static void ctl_expression(int channel, int val)
  277. {
  278.   if (!ctl.trace_playing) 
  279.     return;
  280.   SLsmg_gotorc(8+channel, SLtt_Screen_Cols-12);
  281.   SLsmg_printf("%3d", (val*100)/127);
  282. }
  283.  
  284. static void ctl_panning(int channel, int val)
  285. {
  286.   if (!ctl.trace_playing) 
  287.     return;
  288.   SLsmg_gotorc(8+channel, SLtt_Screen_Cols-8);
  289.   if (val==NO_PANNING)
  290.     SLsmg_write_string("   ");
  291.   else if (val<5)
  292.     SLsmg_write_string(" L ");
  293.   else if (val>123)
  294.     SLsmg_write_string(" R ");
  295.   else if (val>60 && val<68)
  296.     SLsmg_write_string(" C ");
  297.   else
  298.     {
  299.       /* wprintw(dftwin, "%+02d", (100*(val-64))/64); */
  300.       val = (100*(val-64))/64; /* piss on curses */
  301.       if (val<0)
  302.       {
  303.         SLsmg_write_char('-');
  304.         val=-val;
  305.       }
  306.       else SLsmg_write_char('+');
  307.       SLsmg_printf("%02d", val);
  308.     }
  309. }
  310.  
  311. static void ctl_sustain(int channel, int val)
  312. {
  313.   if (!ctl.trace_playing) 
  314.     return;
  315.   SLsmg_gotorc(8+channel, SLtt_Screen_Cols-4);
  316.   if (val) SLsmg_write_char('S');
  317.   else SLsmg_write_char(' ');
  318. }
  319.  
  320. static void ctl_pitch_bend(int channel, int val)
  321. {
  322.   if (!ctl.trace_playing) 
  323.     return;
  324.   SLsmg_gotorc(8+channel, SLtt_Screen_Cols-2);
  325.   if (val>0x2000) SLsmg_write_char('+');
  326.   else if (val<0x2000) SLsmg_write_char('-');
  327.   else SLsmg_write_char(' ');
  328. }
  329.  
  330. static void ctl_reset(void)
  331. {
  332.   int i,j;
  333.   if (!ctl.trace_playing) 
  334.     return;
  335.   for (i=0; i<16; i++)
  336.     {
  337.       SLsmg_gotorc(8+i, 3);
  338.       for (j=0; j<SLtt_Screen_Cols-24; j++)
  339.       SLsmg_write_char('.');
  340.       ctl_program(i, channel[i].program);
  341.       ctl_volume(i, channel[i].volume);
  342.       ctl_expression(i, channel[i].expression);
  343.       ctl_panning(i, channel[i].panning);
  344.       ctl_sustain(i, channel[i].sustain);
  345.       ctl_pitch_bend(i, channel[i].pitchbend);
  346.     }
  347.   _ctl_refresh();
  348. }
  349.  
  350. /***********************************************************************/
  351.  
  352.  
  353. static int ctl_open(int using_stdin, int using_stdout)
  354. {
  355. #ifdef TIOCGWINSZ
  356.   struct winsize size;
  357. #endif
  358.   int i;
  359.   int save_lines, save_cols;
  360.  
  361.   SLtt_get_terminfo();
  362. /*
  363.  * Save the terminfo values for lines and cols
  364.  * then detect the real values.
  365.  */
  366.   save_lines = SLtt_Screen_Rows;
  367.   save_cols = SLtt_Screen_Cols;
  368. #ifdef TIOCGWINSZ
  369.   if (!ioctl(0, TIOCGWINSZ, &size)) {
  370.     SLtt_Screen_Cols=size.ws_col;
  371.     SLtt_Screen_Rows=size.ws_row;
  372.   } else
  373. #endif
  374.   {
  375. #ifdef __EMX__
  376.     int dst[2];
  377.     _scrsize(dst);
  378.     SLtt_Screen_Cols=dst[0];
  379.     SLtt_Screen_Rows=dst[1];
  380. #else
  381.     SLtt_Screen_Cols=atoi(getenv("COLUMNS"));
  382.     SLtt_Screen_Rows=atoi(getenv("LINES"));
  383. #endif    
  384.   }
  385.   if (!SLtt_Screen_Cols || !SLtt_Screen_Rows) {
  386.     SLtt_Screen_Rows = save_lines;
  387.       SLtt_Screen_Cols = save_cols;
  388.   }
  389.   SLang_init_tty(7, 0, 0);
  390.   SLsmg_init_smg();
  391.   SLtt_set_color (20, "Normal", "lightgray", "black");
  392.   SLtt_set_color (21, "HighLight", "white", "black");
  393.   SLtt_set_color (22, "Reverse", "black", "white");
  394.   SLtt_Use_Ansi_Colors = 1;
  395.   SLtt_Term_Cannot_Scroll = 1;
  396.  
  397.   ctl.opened=1;
  398.  
  399.   SLsmg_cls();
  400.  
  401.   ctl_head();
  402.  
  403.   SLsmg_printfrc(3,0, "File:");
  404.   if (ctl.trace_playing)
  405.     {
  406.       SLsmg_printfrc(4,0, "Time:");
  407.       SLsmg_gotorc(4,6+6+1);
  408.       SLsmg_write_char('/');
  409.       SLsmg_gotorc(4,40);
  410.       SLsmg_printf("Voices:    / %d", voices);
  411.     }
  412.   else
  413.     {
  414.       SLsmg_printfrc(4,0, "Playing time:");
  415.     }
  416.   SLsmg_printfrc(4,SLtt_Screen_Cols-20, "Master volume:");
  417.   SLsmg_gotorc(5,0);
  418.   for (i=0; i<SLtt_Screen_Cols; i++)
  419.     SLsmg_write_char('_');
  420.   if (ctl.trace_playing)
  421.     {
  422.       SLsmg_printfrc(6,0, "Ch");
  423.       SLsmg_printfrc(6,SLtt_Screen_Cols-20, "Prg Vol Exp Pan S B");
  424.       SLsmg_gotorc(7,0);
  425.       for (i=0; i<SLtt_Screen_Cols; i++)
  426.       SLsmg_write_char('-');
  427.       for (i=0; i<16; i++)
  428.       {
  429.         SLsmg_printfrc(8+i, 0, "%02d", i+1);
  430.       }
  431.     }
  432.   else
  433.     msg_row = 6;
  434.   _ctl_refresh();
  435.   
  436.   return 0;
  437. }
  438.  
  439. static void ctl_close(void)
  440. {
  441.   if (ctl.opened)
  442.     {
  443.         SLsmg_normal();
  444.         SLsmg_gotorc(SLtt_Screen_Rows - 1, 0);
  445.         SLsmg_refresh();
  446.         SLsmg_reset_smg();
  447.         SLang_reset_tty();
  448.       ctl.opened=0;
  449.     }
  450. }
  451.  
  452. static int ctl_read(int32 *valp)
  453. {
  454.   int c;
  455.  
  456.   if (!SLang_input_pending(0))
  457.     return RC_NONE;
  458.  
  459.   c=SLang_getkey();
  460.     switch(c)
  461.       {
  462.       case 'h':
  463.       case '?':
  464.         ctl_help_mode();
  465.         return RC_NONE;
  466.         
  467.       case 'V':
  468.         *valp=10;
  469.         return RC_CHANGE_VOLUME;
  470.       case 'v':
  471.         *valp=-10;
  472.         return RC_CHANGE_VOLUME;
  473.       case 'q':
  474.         return RC_QUIT;
  475.       case 'n':
  476.         return RC_NEXT;
  477.       case 'p':
  478.         return RC_REALLY_PREVIOUS;
  479.       case 'r':
  480.         return RC_RESTART;
  481.  
  482.       case 'f':
  483.         *valp=play_mode->rate;
  484.         return RC_FORWARD;
  485.       case 'b':
  486.         *valp=play_mode->rate;
  487.         return RC_BACK;
  488.         /* case ' ':
  489.            return RC_TOGGLE_PAUSE; */
  490.       }
  491.   return RC_NONE;
  492. }
  493.  
  494. static int cmsg(int type, int verbosity_level, char *fmt, ...)
  495. {
  496.   va_list ap;
  497.   char p[1000];
  498.   if ((type==CMSG_TEXT || type==CMSG_INFO || type==CMSG_WARNING) &&
  499.       ctl.verbosity<verbosity_level)
  500.     return 0;
  501.   va_start(ap, fmt);
  502.   if (!ctl.opened)
  503.     {
  504.       vfprintf(stderr, fmt, ap);
  505.       fprintf(stderr, "\n");
  506.     }
  507.   else if (ctl.trace_playing)
  508.     {
  509.       switch(type)
  510.       {
  511.         /* Pretty pointless to only have one line for messages, but... */
  512.       case CMSG_WARNING:
  513.       case CMSG_ERROR:
  514.       case CMSG_FATAL:
  515.         SLsmg_gotorc(2,0);
  516.       SLsmg_erase_eol();
  517.       SLsmg_bold();
  518.         vsprintf(p, fmt, ap);
  519.         SLsmg_write_string(p);
  520.       SLsmg_normal();
  521.         _ctl_refresh();
  522.         if (type==CMSG_WARNING)
  523.           sleep(1); /* Don't you just _HATE_ it when programs do this... */
  524.         else
  525.           sleep(2);
  526.         SLsmg_gotorc(2,0);
  527.       SLsmg_erase_eol();
  528.         _ctl_refresh();
  529.         break;
  530.       }
  531.     }
  532.   else
  533.     {
  534.       SLsmg_gotorc(msg_row++,0);
  535.       switch(type)
  536.       {
  537.       default:
  538.         vsprintf(p, fmt, ap);
  539.         SLsmg_write_string(p);
  540.         _ctl_refresh();
  541.         break;
  542.  
  543.       case CMSG_WARNING:
  544.       SLsmg_bold();
  545.         vsprintf(p, fmt, ap);
  546.         SLsmg_write_string(p);
  547.       SLsmg_normal();
  548.         _ctl_refresh();
  549.         break;
  550.         
  551.       case CMSG_ERROR:
  552.       case CMSG_FATAL:
  553.       SLsmg_bold();
  554.         vsprintf(p, fmt, ap);
  555.         SLsmg_write_string(p);
  556.       SLsmg_normal();
  557.         _ctl_refresh();
  558.         if (type==CMSG_FATAL)
  559.           sleep(2);
  560.         break;
  561.       }
  562.     }
  563.  
  564.   va_end(ap);
  565.   return 0;
  566. }
  567.